package com.redwine.app.view.shareDialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.redwine.app.BaseActivity;
import com.redwine.app.R;
import com.redwine.app.utils.Utils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

public class BottomDialog {

    private String title;
    private String link;
    private String imageUrl;
    private String description;
    private CustomDialog customDialog;
    public Activity mActivity;
    private UMWeb web;

    private LinearLayout layout_share_sina;
    private LinearLayout layout_all;
    private LinearLayout layout_share_weixin;
    private LinearLayout layout_share_timeline;
    private TextView cancel;
    private FrameLayout background;

    public BottomDialog(Activity actvity) {
        mActivity = actvity;
        customDialog = new CustomDialog(actvity);
    }

    public void show() {
        if (customDialog.isShowing()) {
            return;
        }
        customDialog.show();
    }


    public BottomDialog toShareParams(String title, String link,
                                      String imageUrl, String description) {
        this.title = title;
        this.link = link;
        this.imageUrl = imageUrl;
        this.description = description;
        initMedia();
        return this;
    }

    private final class CustomDialog extends Dialog {

        CustomDialog(Context context) {
            super(context, R.style.BottomDialog);
            init();
        }

        private void init() {
            setContentView(R.layout.activity_share);
            setCancelable(true);
            setCanceledOnTouchOutside(true);
            getWindow().setGravity(Gravity.BOTTOM);
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            layout_share_sina = (LinearLayout) findViewById(R.id.layout_share_sina);
            layout_all = (LinearLayout) findViewById(R.id.content);
            layout_share_weixin = (LinearLayout) findViewById(R.id.layout_share_weixin);
            layout_share_timeline = (LinearLayout) findViewById(R.id.layout_share_timeline);
            cancel = (TextView) findViewById(R.id.cancel);
            background = (FrameLayout) findViewById(R.id.background);
            background.setOnClickListener(mOnClickListener);
            layout_share_sina.setOnClickListener(mOnClickListener);
            layout_share_weixin.setOnClickListener(mOnClickListener);
            layout_share_timeline.setOnClickListener(mOnClickListener);
            cancel.setOnClickListener(mOnClickListener);
        }
    }


    public View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.cancel:
                    customDialog.dismiss();
                    break;
                case R.id.background:
                    customDialog.dismiss();
                    break;

                case R.id.layout_share_sina:
                    if (Utils.isFastClick(1000)) {
                        return;
                    }
                    shareMedia(SHARE_MEDIA.SINA);
                    break;
                case R.id.layout_share_weixin:
                    if (Utils.isFastClick(1000)) {
                        return;
                    }
                    shareMedia(SHARE_MEDIA.WEIXIN);
                    break;
                case R.id.layout_share_timeline://微信朋友圈
                    if (Utils.isFastClick(1000)) {
                        return;
                    }
                    shareMedia(SHARE_MEDIA.WEIXIN_CIRCLE);
                    break;
            }
        }
    };


    protected void shareMedia(SHARE_MEDIA share_media) {
        layout_all.setVisibility(View.GONE);
        ((BaseActivity) mActivity).showWaiting("跳转中...");
        new ShareAction(mActivity)
                .withMedia(web)
                .setPlatform(share_media)
                .setCallback(shareListener).share();
    }


    private void initMedia() {
        web = new UMWeb(link);
        web.setTitle(title);
        web.setThumb(new UMImage(mActivity, imageUrl));
        web.setDescription(description);

    }


    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Utils.showToast("分享成功！");
            ((BaseActivity) mActivity).dismissWaiting();
            if (null != customDialog && customDialog.isShowing()) {
                customDialog.dismiss();
            }
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Utils.showToast("分享失败请重试！");
            ((BaseActivity) mActivity).dismissWaiting();
            if (null != customDialog && customDialog.isShowing()) {
                customDialog.dismiss();
            }
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Utils.showToast("取消分享！");
            ((BaseActivity) mActivity).dismissWaiting();
            if (null != customDialog && customDialog.isShowing()) {
                customDialog.dismiss();
            }
        }
    };

}
